<template>
	<jk-dialog
		v-model="showDialog"
		title="单据编号规则-详情"
		width="500px"
		:show-content-loading="showLoading"
		append-to-body
		:show-cancel-btn="false"
		:show-confirm-btn="false"
		:show-close-btn="true"
		@on-close="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<el-form ref="ruleForm" label-width="110px">
			<el-row>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="类型">
						<span class="read-only-2">{{ ruleForm.typeName }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="前缀">
						<span class="read-only-2">{{ ruleForm.prefix }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="日期规则">
						<span class="read-only-2">{{ ruleForm.date }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="流水号长度">
						<span class="read-only-2">{{ ruleForm.numLength }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="版本">
						<span class="read-only-2">{{ ruleForm.version }}</span>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<jk-divider-card title="模拟生成" placement="center" icon="el-icon-set-up" class="margin-top-10">
			<div class="code-rule-container">
				<div class="code-rule-container-groups">
					<div class="code-rule-container_column">
						<span class="code-rule-container_column-title">前缀</span>
						<el-input readonly :value="ruleForm.prefix"></el-input>
					</div>
					<div class="code-rule-container_column">
						<span class="code-rule-container_column-title">日期规则</span>
						<el-input readonly :value="transferDate(ruleForm.date)"></el-input>
					</div>
					<div class="code-rule-container_column">
						<span class="code-rule-container_column-title">流水号</span>
						<el-input readonly :value="transferNum(ruleForm.numLength)"></el-input>
					</div>
				</div>
				<div class="code-rule-container_column-demo">如：{{ `${ruleForm.prefix}${ transferDate(ruleForm.date) }${ transferNum(ruleForm.numLength) }` }}</div>
			</div>
		</jk-divider-card>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { spinGenerateCodeSettingGet } from '@/api/basicData/orderCodeSetting';
    import { formatDate, formatMonth, formatTime } from '../../../utils/jk/jk-tool';

    export default {
        name: 'OrderCodeSetting',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                showDialog: false,
                showLoading: true,
                ruleForm: {}
            };
        },
        methods: {
            transferNum(len) {
                let str = '';
                for (let i = 0; i < len; i++) {
                    if (i === len - 1) {
                        str += '1';
                    } else {
                        str += '0';
                    }
                }
                return str;
            },
            transferDate(e) {
                let str = '';
                if (e === 'yyyy') {
                    str = new Date().getFullYear();
                } else if (e === 'yyyyMM') {
                    str = formatMonth(new Date(), '');
                } else if (e === 'yyyyMMdd') {
                    str = formatDate(new Date(), '');
                } else if (e === 'yyyyMMddHHmmss') {
                    str = formatTime(new Date(), '');
                }
                return str;
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getDetailRequest() {
                this.showLoading = true;
                return spinGenerateCodeSettingGet({ id: this.dataId }).then(res => {
                    this.showLoading = false;
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal && this.dataId) {
                    this.getDetailRequest();
                }
            }
        }
    };
</script>
<style scoped lang="scss">
.code-rule-container {
    //border: solid 1px #dcdee2;
    padding: 12px;
    .code-rule-container-groups {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 8px;
        .code-rule-container_column {
            width: calc(100% / 3);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin: 0 1px;
            .code-rule-container_column-title {

            }
        }
    }
    .code-rule-container_column-demo {
        width: 100%;
        font-size: 14px;
        font-weight: bold;
        color: #2d8cf0;
        text-align: center;
    }
}
</style>
